const path = require('path')

function resolve(dir) {
  return path.join(__dirname, dir)
}

// 导出API代理
function getProxyObj() {
  let ret = {}, list;
  if (process.env.VUE_APP_PROXY) {
    try {
      list = JSON.parse(process.env.VUE_APP_PROXY.replace(/'/g, '"'));
    } catch (e) {
      list = '';
      ret = null;
    }
  } else {
    list = '';
    ret = null;
  }
  for (const [prefix, target] of list) {
    ret[prefix] = {
      target: target,
      changeOrigin: true,
      ws: false,
      secure: false,
      pathRewrite: {
        [`^${prefix}`]: ''
      }
    }
  }
  return ret;
}

module.exports = {
  lintOnSave: process.env.NODE_ENV === 'development',
  transpileDependencies: ['uview-ui'],
  configureWebpack: {
    resolve: {
      alias: {
        '@': resolve('src'),
        '@static': resolve('src/static'),
        '@api': resolve('src/api'),
        '@views': resolve('src/views'),
        '@utils': resolve('src/utils'),
        '@components': resolve('src/components')
      }
    }
  },
  chainWebpack: (config) => {
    // 发行或运行时启用了压缩时会生效
    config.optimization.minimizer('terser').tap((args) => {
      args[0].terserOptions.output = {
        comments: false
      };
      const compress = args[0].terserOptions.compress
      // 非 App 平台移除 console 代码(包含所有 console 方法，如 log,debug,info...)
      compress.drop_console = false
      compress.pure_funcs = [
        '__f__', // App 平台 vue 移除日志代码
        'console.debug' // 可移除指定的 console 方法
      ]
      return args
    })
  },
  devServer: {
    host: '127.0.0.1',
    port: '8080',
    proxy: getProxyObj(),
    hot: true,
    open: true,
    progress: true,
    disableHostCheck: true
  }
}
